package com.tgu.home_action.controller;

import com.tgu.home_action.pojo.*;
import com.tgu.home_action.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Api(tags = "公告管理")
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @ApiOperation(value = "查询公告（可通过id,publisherId,show,title模糊查询）")
    @GetMapping
    public ResponseData<List<Notice>> getNotices(@ApiParam(name = "")  Notice notice,
                                                 @ApiIgnore @ModelAttribute("familyUser") Family familyUser,
                                                 @ApiIgnore @ModelAttribute("volunteerUser") Volunteer volunteerUser,
                                                 @ApiIgnore @ModelAttribute("adminUser") Admin adminUser) {
        return noticeService.getNotices(notice);
    }

    @ApiOperation(value = "查询banner（可通过id,noticeId,title模糊查询）")
    @GetMapping("/banner")
    public ResponseData<List<Banner>> getBanners( Banner banner,
                                                 @ApiIgnore @ModelAttribute("familyUser") Family familyUser,
                                                 @ApiIgnore @ModelAttribute("volunteerUser") Volunteer volunteerUser,
                                                 @ApiIgnore @ModelAttribute("adminUser") Admin adminUser) {

        System.out.println("RoleId"+banner.getRoleId());
        return noticeService.getBanners(banner);
    }

    @ApiOperation("管理员查询公告")
    @PreAuthorize("hasAnyRole('superAdmin','admin','districtAdmin')")
    @GetMapping("/admin")
    public ResponseData<Page<Notice>> adminGetNotices(Notice notice) {
        return noticeService.adminGetNotices(notice);
    }

    @ApiOperation("新建公告")
    @PreAuthorize("hasAnyRole('superAdmin','admin','districtAdmin')")
    @PostMapping("/admin")
    public ResponseData<Object> postNotice(@Validated(Notice.insert.class) @RequestBody Notice notice, @ApiIgnore @ModelAttribute("adminUser") Admin adminUser) {
        return noticeService.postNotice(notice, adminUser);
    }

    @ApiOperation("修改公告")
    @PreAuthorize("hasAnyRole('superAdmin','admin','districtAdmin')")
    @PutMapping("/admin")
    public ResponseData<Object> updateNotice(@Validated(Notice.update.class) @RequestBody Notice notice, @ApiIgnore @ModelAttribute("adminUser") Admin adminUser) {
        return noticeService.updateNotice(notice);
    }

    @ApiOperation("删除公告")
    @PreAuthorize("hasAnyRole('superAdmin','admin','districtAdmin')")
    @DeleteMapping("/admin")
    public ResponseData<Object> delNotice(@RequestParam Integer id) {
        return noticeService.delNotice(id);
    }

    @ApiOperation("通过ID查询公告")
    @GetMapping("/id")
    public ResponseData<Notice> getNoticeById(@RequestParam("id") Integer id) {
        return noticeService.getNoticeById(id);
    }

    @ApiOperation("管理员查询Banner")
    @PreAuthorize("hasAnyRole('superAdmin','admin','districtAdmin')")
    @GetMapping("/banner/admin")
    public ResponseData<Object> adminGetBanner(Notice notice) {
        return noticeService.adminGetBanner(notice);
    }

    @ApiOperation("新建Banner")
    @PreAuthorize("hasAnyRole('superAdmin','admin','districtAdmin')")
    @PostMapping("/banner/admin")
    public ResponseData<Object> postBanner(@Validated(Banner.insert.class) @RequestBody Banner banner, @ApiIgnore @ModelAttribute("adminUser") Admin adminUser) {
        return noticeService.postBanner(banner, adminUser);
    }

    @ApiOperation("修改Banner")
    @PreAuthorize("hasAnyRole('superAdmin','admin','districtAdmin')")
    @PutMapping("/banner/admin")
    public ResponseData<Object> updateBanner(@Validated(Banner.update.class) @RequestBody Banner banner, @ApiIgnore @ModelAttribute("adminUser") Admin adminUser) {
        return noticeService.updateBanner(banner);
    }

    @ApiOperation("删除Banner")
    @PreAuthorize("hasAnyRole('superAdmin','admin','districtAdmin')")
    @DeleteMapping("/banner/admin")
    public ResponseData<Object> delBanner(@RequestParam Integer id) {
        return noticeService.delBanner(id);
    }

    @ApiOperation("通过ID查询Banner")
    @GetMapping("/banner/id")
    public ResponseData<Banner> getBannerById(@RequestParam Integer id) {
        return noticeService.getBannerById(id);
    }

}
